﻿PuppetPlay.AppBar = function(config){
    Kinetic.Group.apply(this, [config]);
    var setting = PuppetPlay.Setting;
    var that = this;

    var pulley01 = new PuppetPlay.Pulley({
        id: 'pulley01',
        x: 80,
        y: 80,
        afterRotate: config.leftPulleyCallback
    });
    this.pulley01 = pulley01;
    this.add(pulley01);

    
    var pulley02 = new PuppetPlay.Pulley({
        id: 'pulley02',
        x: this.attrs.width - 80,
        y: 80,
        afterRotate: config.rightPulleyCallback
    });
    this.pulley02 = pulley02;
    this.add(pulley02);
    
    var stick01 = new PuppetPlay.ControlStick({
        id: 'stick01',
        x: setting.MOVEHANDLE_CENTER_OFFSET_X,
        minX: setting.HORIZONTAL_TRACK_MARGIN,
        pointerDrawFunc: config.leftPointerDrawFunc,
        afterRotate: config.leftStickRotateCallback,
        beforeMove: function (moveXY) {
            var x01 = stick01.getPosition().x + moveXY.x;
            if (x01 <= stick01.attrs.minX) {
                //stick01.setPosition(stick01.attrs.minX, stick01.attrs.y);
                return false;
            } else if (x01 >= stick02.getPosition().x - 220) {
                //stick01.setPosition(stick02.getPosition().x - 220, stick01.attrs.y);
                return false;
            } else {
                return true;
            }
        },
		afterMove: config.leftStickMoveCallback
    });
    this.stick01 = stick01;
    this.add(stick01);
    
    var stick02 = new PuppetPlay.ControlStick({
        id: 'stick02',
        x: this.attrs.width - setting.MOVEHANDLE_CENTER_OFFSET_X,
        maxX: this.attrs.width - setting.HORIZONTAL_TRACK_MARGIN,
        pointerDrawFunc: config.rightPointerDrawFunc,
        afterRotate: config.rightStickRotateCallback,
        beforeMove: function (moveXY) {
            var x02 = stick02.getPosition().x + moveXY.x;
            if (x02 >= stick02.attrs.maxX) {
                //stick02.setPosition(stick02.attrs.maxX, stick02.attrs.y);
                return false;
            } else if (x02 <= stick01.getPosition().x + 220) {
                //stick02.setPosition(stick01.getPosition().x + 220, stick02.attrs.y);
                return false;
            } else {
                return true;
            }
        },
		afterMove: config.rightStickMoveCallback
    }); 
    this.stick02 = stick02;
    this.add(stick02);
    /*
    this.add(new Kinetic.Shape({
        x: 500,
        y: 65,
        drawFunc: function (context) {
            var gradient = context.createLinearGradient(0, 0, 0, 65);
            
            gradient.addColorStop(0, 'rgba(96, 96, 96, 0.7)');
            gradient.addColorStop(1, 'rgba(0, 0, 0, 0.7)');
            context.fillStyle = gradient;
            context.beginPath();
            context.arc(this.attrs.x, this.attrs.y, 65, Math.PI, Math.PI * 2);
            context.closePath();
            context.fill();
        }
    }));
    */


};

PuppetPlay.AppBar.prototype = {
    getLeftPulleyJointPoint: function (x, y) {
        return this.pulley01.getJointPoint(x, y);
    },
    getRightPulleyJointPoint: function (x, y) {
        return this.pulley02.getJointPoint(x, y);
    },
    getLeftStickJointPoint: function () {
        return this.stick01.getJointPoint();
    },
    getRightStickJointPoint: function () {
        return this.stick02.getJointPoint();
    }
};

Kinetic.Global.extend(PuppetPlay.AppBar, Kinetic.Group);